package thu.infinity.facebook;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.apache.thrift.TException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import thu.infinity.commons.AggregatorHandler;
import thu.infinity.commons.Task;
import thu.infinity.commons.TaskScheduler;
import thu.infinity.facebook.FacebookUser;
import thu.infinity.facebook.FacebookError;
import thu.infinity.facebook.FacebookUtil;
import thu.infinity.commons.Para;
import thu.infinity.thrift.Constants;
import thu.infinity.thrift.DirEntry;
import thu.infinity.thrift.FriendAlbum;

public class QueryUserTask extends Task {
	FacebookUser fbUser;
	
	public QueryUserTask(long expireTime, long period) {
		super(expireTime, period);
		// TODO Auto-generated constructor stub
		this.isPeriodical = true;
	}

	public QueryUserTask(FacebookUser fbUser, AggregatorHandler handler) {
		this.fbUser = fbUser;
		this.handler = handler;
		this.isPeriodical = false;
	}
	@Override
	protected Task genNextTask() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void execute() {
		// TODO Auto-generated method stub
		if (fbUser.newUser) {
			try {
				String friendRes;
				friendRes = fbUser.fbconn.request("me/friends");
	
				JSONObject friendJson = FacebookUtil.parseJson(friendRes);
				JSONArray friendData = friendJson.getJSONArray("data");
	
				int frndLen = friendData.length();
				
				System.out.println("Length " + (frndLen + 1));
				fbUser.friends = new String[frndLen + 1];
				fbUser.friendsNewUser = new boolean[frndLen + 1];
				fbUser.friendsAlbumNum = new int[frndLen + 1];
				fbUser.updatedTime = new String[frndLen + 1];
				
				fbUser.friends[0] = fbUser.uid;
				fbUser.friendsNewUser[0] = true;
				
				for (int i = 1; i < fbUser.friends.length; i++) {
					JSONObject data = friendData.getJSONObject(i - 1);
					fbUser.friends[i] = data.getString("id");
					fbUser.friendsNewUser[i] = true;
					System.out.println("Friend " + i + " " + fbUser.friends[i] + " ");
				}
			} catch (MalformedURLException e1) {
			e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (FacebookError e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
			if (fbUser.friends == null) {
				fbUser.friends = new String[0];			
			}	
			
			for (int i = 0; i < fbUser.friends.length; i++) {
				String fid = fbUser.friends[i];
				String fn = Util.friendAlbumName(fbUser.uid, fid);
				File f = Util.newFile(fn);

				if (i == 0) {
					fbUser.updatedTime[i] = "";
				} else {
					fbUser.updatedTime[i] = Para.DefaultStartTime;
				}
				
				if (f.exists()) {
					FriendAlbum fa = null;
					try {
						fa = FacebookUtil.readFriendAlbumFromFile(f);
					} catch (TException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					fbUser.updatedTime[i] = fa.updatedTime;
					System.out.println("File exists");						
				}				
			}
			// invoke QueryFriendTask
			QueryFriendsTask qryFriends = new QueryFriendsTask(fbUser, handler);
			handler.getScheduler().AddTask(qryFriends);
					
			//Write the friend list to file
			String fn = Util.friendListName(fbUser.uid);
			System.out.println(fn);
			File f = Util.newFile(fn);
			FileOutputStream fos;
			try {
				fos = new FileOutputStream(f);
				ObjectOutputStream oos = new ObjectOutputStream (fos);
				oos.writeObject(fbUser.friends);
				oos.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("Write the friend list to file End.");
			String curTime = Util.getCurTimeString();
			
			//Add notification for the client
			fbUser.newUser = false;
		}
	}

	


	@Override
	public boolean checkCondition() {
		// TODO Auto-generated method stub
		return true;
	}

}
